angular.module('Controller')

.controller('QgyLayoutPictureCtrl', ($scope,NgTableParams,wfAlert,$localStorage,commonUtil,commonConstant,$rootScope,$uibModal,$http) ->
    $scope.type=$rootScope.pageType
    $scope.data=$rootScope.pageParams
    $scope.enabledList=commonConstant.enabledList(true)
    $scope.tableParams = new NgTableParams(
            {page: 1, count: commonConstant.pageTableCount},
            {
                counts: [5, 10, 20],
                getData: ($defer, params)->
                    $scope.doQuery($defer,params)
                    return ;
            }
    )
    $scope.doQuery=($defer, params)->
        requestData={
            page: params.page() - 1,
            size: params.count()
        }
        $http.get('/layoutPicture/'+$scope.data.id,params:requestData)
        .success (resp)->
            if resp.code==0
                $scope.tableData=resp.message.content
                params.total(resp.message.totalElements)
                $defer.resolve($scope.tableData)
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    _.extend $scope,
        newData: ->
            data={layoutId:$scope.data.id,type:1,enabled:true}
            info={
                apartmentName:$scope.data.apartmentName
                layoutName:$scope.data.name
            }
            showModal('new',info,data)
        editData:(item)->
            theData=angular.copy(item)
            info={
                apartmentName:$scope.data.apartmentName
                layoutName:$scope.data.name
            }
            showModal('edit',info,theData)
        resetSearch: ->
            initSearchData()
        queryData: (keepPage)->
            if(keepPage != true)
                $scope.tableParams.page(1)
            $scope.tableParams.reload()
        removeData:(item)->
            $http.delete('/layoutPicture/'+item.id)
            .success((resp)->
                if resp.code==0
                    wfAlert.toastInfo('房型图片删除成功')
                    $scope.queryData()
                else
                    wfAlert.toastError(resp.message)
            )
    showModal=(type,info,data)->
        $scope.modal = $uibModal.open({
            animation: true,
            appendTo: $("#modalContent:eq(0)"),
            controller: 'QgyLayoutPictureEditCtrl',
            templateUrl: 'templates/project-manage/apartment-layout-manage/layout-picture-modal.html',
            resolve: {
                items:  ()->{type: type,info:info, data: data}
            }
        })
        $scope.modal.result.then((modalData)->
            $scope.queryData()
        )
)